Advertisement
Guest User

Windows Forms TreeView Hierarchy Binding using IHierarchyData/IHierarchyEnumerable

a guest
Mar 20th, 2010
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Web.UI;
  5. using System.Windows.Forms;
  6.  
  7. namespace TreeHierarchyTest
  8. {
  9.     public partial class TreeViewHierarchyBinding : Component
  10.     {
  11.         private IHierarchyData dataSource;
  12.         private TreeView treeView;
  13.         private Dictionary<TreeNode, IHierarchyData> nodeDictionary = new
  14.             Dictionary<TreeNode, IHierarchyData>();
  15.  
  16.         public TreeViewHierarchyBinding()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         public TreeViewHierarchyBinding(IContainer container)
  22.         {
  23.             container.Add(this);
  24.  
  25.             InitializeComponent();
  26.         }
  27.  
  28.         #region TreeView Events
  29.  
  30.         private void RegisterEvents(TreeView tv)
  31.         {
  32.             tv.BeforeExpand += TreeViewBeforeExpand;
  33.         }
  34.  
  35.         private void UnregisterEvents(TreeView tv)
  36.         {
  37.             tv.BeforeExpand -= TreeViewBeforeExpand;
  38.         }
  39.  
  40.         private void TreeViewBeforeExpand(object sender, TreeViewCancelEventArgs e)
  41.         {
  42.             if (e.Node.Checked)
  43.             {
  44.                 return;
  45.             }
  46.             IHierarchyData hierarchyData;
  47.             if (nodeDictionary.TryGetValue(e.Node, out hierarchyData))
  48.             {
  49.                 PopulateChildNodes(e.Node.Nodes, hierarchyData.GetChildren());
  50.                 e.Node.Checked = true;
  51.             }
  52.         }
  53.  
  54.         #endregion
  55.  
  56.         #region TreeView Population
  57.  
  58.         private void PopulateChildNodes(TreeNodeCollection parentCollection,
  59.             IHierarchicalEnumerable children)
  60.         {
  61.             parentCollection.Clear();
  62.             foreach (object child in children)
  63.             {
  64.                 IHierarchyData childData = children.GetHierarchyData(child);
  65.                 TreeNode childNode = new TreeNode(childData.ToString());
  66.                 if (childData.HasChildren)
  67.                 {
  68.                     childNode.Nodes.Add("Dummy");   // Make expandable
  69.                 }
  70.                 nodeDictionary.Add(childNode, childData);
  71.                 parentCollection.Add(childNode);
  72.             }
  73.         }
  74.  
  75.         private void UpdateRootNodes(TreeView tv, IHierarchyData hierarchyData)
  76.         {
  77.             if (tv == null)
  78.             {
  79.                 return;
  80.             }
  81.             tv.Nodes.Clear();
  82.             if (hierarchyData != null)
  83.             {
  84.                 IHierarchicalEnumerable roots = hierarchyData.GetChildren();
  85.                 PopulateChildNodes(tv.Nodes, roots);
  86.             }
  87.         }
  88.  
  89.         #endregion
  90.  
  91.         [Browsable(false)]
  92.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  93.         public IHierarchyData DataSource
  94.         {
  95.             get { return dataSource; }
  96.             set
  97.             {
  98.                 if (value != dataSource)
  99.                 {
  100.                     dataSource = value;
  101.                     nodeDictionary.Clear();
  102.                     UpdateRootNodes(treeView, value);
  103.                 }
  104.             }
  105.         }
  106.  
  107.         [Category("Behavior")]
  108.         [DefaultValue(null)]
  109.         [Description("Specifies the TreeView that the hierarchy should be bound to.")]
  110.         public TreeView TreeView
  111.         {
  112.             get { return treeView; }
  113.             set
  114.             {
  115.                 if (value != treeView)
  116.                 {
  117.                     if (treeView != null)
  118.                     {
  119.                         UnregisterEvents(treeView);
  120.                     }
  121.                     treeView = value;
  122.                     nodeDictionary.Clear();
  123.                     RegisterEvents(value);
  124.                     UpdateRootNodes(treeView, dataSource);
  125.                 }
  126.             }
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement